package com.chixing.service.impl;

import com.chixing.mapper.PointMapper;
import com.chixing.mapper.VipMapper;
import com.chixing.pojo.*;
import com.chixing.service.PointService;
import com.chixing.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PointServiceImpl implements PointService {
    @Autowired
    private PointMapper pointMapper;
    @Autowired
    private VipMapper vipMapper;
    @Override
    public ServerResponse getByCusId(Integer cusId) {
        VipExample vipExample = new VipExample();
        vipExample.createCriteria().andCusIdEqualTo(cusId);

        List<Vip> vipList = vipMapper.selectByExample(vipExample);
        if (vipList !=null && vipList.size()>0){
            Vip vip = vipList.get(0);
            PointExample pointExample = new PointExample();
            pointExample.createCriteria().andVipIdEqualTo(vip.getVipId());
            List<Point> pointList = pointMapper.selectByExample(pointExample);
            if (pointList !=null && pointList.size()>0){
                Point point = pointList.get(0);
                return ServerResponse.getSuccess(point);
            }
        }

        return ServerResponse.getFail(null);
    }

    @Override
    public ServerResponse update(Point point) {
        int row = pointMapper.updateByPrimaryKey(point);
        if (row>0)
            return ServerResponse.updateSuccess(row);
        return ServerResponse.updateFail(null);
    }

    @Override
    public ServerResponse save(Point point) {
        int row = pointMapper.insert(point);
        if (row>0)
            return ServerResponse.saveSuccess(row);
        return ServerResponse.saveFail(null);
    }

    @Override
    public ServerResponse getVipId(Integer vipId) {
        PointExample pointExample = new PointExample();
        pointExample.createCriteria().andVipIdEqualTo(vipId);
        Point point = pointMapper.selectByExample(pointExample).get(0);
        if (point !=null)
            return ServerResponse.getSuccess(point);
        return ServerResponse.getFail(null);
    }

    @Override
    public ServerResponse getByCoupon(Integer cusId, Integer status) {
        VipExample vipExample = new VipExample();
        vipExample.createCriteria().andCusIdEqualTo(cusId);

        List<Vip> vipList = vipMapper.selectByExample(vipExample);
        if (vipList !=null && vipList.size()>0){
            Vip vip = vipList.get(0);
            PointExample pointExample = new PointExample();
            pointExample.createCriteria().andVipIdEqualTo(vip.getVipId()).andPointStatusEqualTo(status);
            List<Point> pointList = pointMapper.selectByExample(pointExample);
            if (pointList !=null && pointList.size()>0){
                Point point = pointList.get(0);
                return ServerResponse.getSuccess(point);
            }
        }

        return ServerResponse.getFail(null);
    }
}
